using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace The_LarX
{
    public class GlobalsManager : Component
    {
        public static string GameName = "Sample";

        private NamedStringCollection strings = new NamedStringCollection();
        public NamedStringCollection Strings
        {
            get { return strings; }
            set { strings = value; }
        }

        private NamedIntCollection ints = new NamedIntCollection();
        public NamedIntCollection Ints
        {
            get { return ints; }
            set { ints = value; }
        }

        private NamedFloatCollection floats = new NamedFloatCollection();
        public NamedFloatCollection Floats
        {
            get { return floats; }
            set { floats = value; }
        }

        private NamedBoolCollection bools = new NamedBoolCollection();
        public NamedBoolCollection Bools
        {
            get { return bools; }
            set { bools = value; }
        }

        private NamedStringCollection lastStrings;
        private NamedIntCollection lastInts;
        private NamedFloatCollection lastFloats;
        private NamedBoolCollection lastBools;

        public override void SetToPlayMode()
        {
            base.SetToPlayMode();

            lastStrings = new NamedStringCollection();
            lastInts = new NamedIntCollection();
            lastFloats = new NamedFloatCollection();
            lastBools = new NamedBoolCollection();

            foreach (NamedString String in Engine.Globals.Strings)
            {
                lastStrings.Add(new NamedString(String.Name, String.String));
            }
            foreach (NamedInt Int in Engine.Globals.Ints)
            {
                lastInts.Add(new NamedInt(Int.Name, Int.Int));
            }
            foreach (NamedFloat Float in Engine.Globals.Floats)
            {
                lastFloats.Add(new NamedFloat(Float.Name, Float.Float));
            }
            foreach (NamedBool Bool in Engine.Globals.Bools)
            {
                lastBools.Add(new NamedBool(Bool.Name, Bool.Bool));
            }
        }
        public override void SetToInitialState()
        {
            base.SetToInitialState();

            if (lastBools != null)
            {
                Engine.Globals.Bools = lastBools;
                Engine.Globals.Floats = lastFloats;
                Engine.Globals.Ints = lastInts;
                Engine.Globals.Strings = lastStrings;
            }
        }

        public override void SerializeData()
        {
            base.SerializeData();

            Data.AddData("NumbefOfStrings", Engine.Globals.Strings.Count);
            Data.AddData("NumbefOfInts", Engine.Globals.Ints.Count);
            Data.AddData("NumbefOfFloats", Engine.Globals.Floats.Count);
            Data.AddData("NumbefOfBools", Engine.Globals.Bools.Count);

            Data.AddData("Rendering.BackgroundColor", Engine.RenderManager.BackgroundColor);

            for (int j = 0; j < Engine.Globals.Strings.Count; j++)
            {
                Data.AddData("String.Name." + j.ToString(), Engine.Globals.Strings[j].Name);
                Data.AddData("String.String." + j.ToString(), Engine.Globals.Strings[j].String);
            }
            for (int j = 0; j < Engine.Globals.Ints.Count; j++)
            {
                Data.AddData("Int.Name." + j.ToString(), Engine.Globals.Ints[j].Name);
                Data.AddData("Int.Int." + j.ToString(), Engine.Globals.Ints[j].Int);
            }
            for (int j = 0; j < Engine.Globals.Floats.Count; j++)
            {
                Data.AddData("Float.Name." + j.ToString(), Engine.Globals.Floats[j].Name);
                Data.AddData("Float.Float." + j.ToString(), Engine.Globals.Floats[j].Float);
            }
            for (int j = 0; j < Engine.Globals.Bools.Count; j++)
            {
                Data.AddData("Bool.Name." + j.ToString(), Engine.Globals.Bools[j].Name);
                Data.AddData("Bool.Bool." + j.ToString(), Engine.Globals.Bools[j].Bool);
            }
        }

        public override void DeserializeData()
        {
            base.DeserializeData();

            int NumberOfStrings = Data.GetData<int>("NumbefOfStrings");
            int NumberOfInts = Data.GetData<int>("NumbefOfInts");
            int NumberOfFloats = Data.GetData<int>("NumbefOfFloats");
            int NumberOfBools = Data.GetData<int>("NumbefOfBools");

            Engine.RenderManager.BackgroundColor = Data.GetData<Color>("Rendering.BackgroundColor");

            for (int j = 0; j < NumberOfStrings; j++)
            {
                Engine.Globals.Strings.Add(new NamedString(
                    Data.GetData<string>("String.Name." + j.ToString()),
                    Data.GetData<string>("String.String." + j.ToString())));
            }
            for (int j = 0; j < NumberOfInts; j++)
            {
                Engine.Globals.Ints.Add(new NamedInt(
                    Data.GetData<string>("Int.Name." + j.ToString()),
                    Data.GetData<int>("Int.Int." + j.ToString())));
            }
            for (int j = 0; j < NumberOfFloats; j++)
            {
                Engine.Globals.Floats.Add(new NamedFloat(
                    Data.GetData<string>("Float.Name." + j.ToString()),
                    Data.GetData<float>("Float.Float." + j.ToString())));
            }
            for (int j = 0; j < NumberOfBools; j++)
            {
                Engine.Globals.Bools.Add(new NamedBool(
                    Data.GetData<string>("Bool.Name." + j.ToString()),
                    Data.GetData<bool>("Bool.Bool." + j.ToString())));
            }
        }

    }
}